package cn.fl.customer.utils;

import java.security.KeyFactory;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.cmm.base.framework.utils.PropertiesUtils;
import cn.fl.customer.utils.FaceSaasDataStructure.UploadOptions;
import cn.fl.customer.utils.FaceSaasDataStructure.UserInfo;
import fl.customer.facade.customer.constant.CustomerConstant;
import net.sf.json.JSONObject;

public class YiTuClient {
	private static final Logger logger = LoggerFactory.getLogger(YiTuClient.class);
	
	private String pemPath;
	private final String registerUserInfoUrl = PropertiesUtils.getProperty(CustomerConstant.CUSTOMER_CUSTOMER_PROPERTIES_YT_REG_USR_INFO_URL);
	private String ip = null;

	public YiTuClient() {

	}

	/**
	 * 设置各种参数
	 */
	public void setIp(final String ip) {
		this.ip = "http://" + ip;
	}

	public void setPemPath(String pemPath) throws Exception {
		this.pemPath = pemPath;
		Security.addProvider(new BouncyCastleProvider());
		KeyFactory factory = KeyFactory.getInstance("RSA", "BC");
		HttpRequestHelper.publicKey = EncryptionHelper.RSAHelper.generatePublicKey(factory, this.pemPath);
	}
	/**
	 * 100.1 用户信息上传接口,上传用户的登记信息（照片，特征或身份信息）到依图服务器
	 * 
	 * @param accessId
	 *            客户标识
	 * @param accessKey
	 *            客户密钥
	 * @param sessionId
	 *            会话标识
	 * @param mode
	 *            上传模式，包括如下几种：1: 上传图片模式(默认) 2: 上传特征模式(内部) 3: 上传身份信息模式
	 * @param userInfo
	 *            用户信息,包括图片内容，客户姓名，身份证号等
	 * @param options
	 *            上传选项，包括图片类型，开启自动旋转等
	 * @param userDefinedContent
	 *            用户自定义内容
	 */
	public String registerUserInfo(String accessId, String accessKey, String sessionId, int mode, UserInfo userInfo,
			UploadOptions options) throws Exception {
		String response = null;
		if (HttpRequestHelper.publicKey != null && this.ip != null) {

			/**
			 * 设置必要的body参数
			 */
			Map<String, Object> requestData = new HashMap<String, Object>();
			Map<String, Object> userInfoMap = new HashMap<String, Object>();
			Map<String, Object> optionsMap = new HashMap<String, Object>();
			
			userInfoMap.put("name", userInfo.getName());
			if (StringUtils.isNotEmpty(userInfo.getCitizenId())) {
				userInfoMap.put("citizen_id", userInfo.getCitizenId());
			}
			if (mode != 3) {
				userInfoMap.put("image_content", userInfo.getImageContent());
				optionsMap.put("image_type", options.getImageType());
			}
			optionsMap.put("auto_rotate", options.isAutoRotate());
			optionsMap.put("idcard_confirm", options.isIdcardConfirm());
			optionsMap.put("remove_watermark", options.isRemoveWatermark());
			optionsMap.put("check_repo", options.isCheckRepo());
			requestData.put("session_id", sessionId);
			requestData.put("mode", mode);
			
			JSONObject jobj=JSONObject.fromObject(optionsMap);//option
			JSONObject uuJson=JSONObject.fromObject(userInfoMap);//user
			
			JSONObject reque=JSONObject.fromObject(requestData);//转换成json
			reque.put("user_info", uuJson);
			reque.put("options", jobj);
            String data=reque.toString();
            logger.info("data:***"+data);
		    response = HttpRequestHelper.sendPost(this.ip + this.registerUserInfoUrl, accessId, accessKey, data
			);
		} else {
			response = "请先配置url和pem";
		}
		return response;
	}

	
	
}
